package cn.tedu.file;

import java.io.*;

/**本类用于字节输入流*/
public class TestIn {
    public static void main(String[] args) {
//        method1();//用于测试普通字节输入流FIS
        method2();//用于测试高效字节输入流BufferedInputStream
    }

    private static void method2() {
        InputStream in2 = null;
        try {
            InputStream in = new BufferedInputStream(new FileInputStream(new File("F:\\ready\\1.txt")));
            in2 = new BufferedInputStream(new FileInputStream("F:\\ready\\1.txt"));

            int b;
            while ((b = in2.read()) !=-1){
                System.out.println(b);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                in2.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    private static void method1() {
        //4.定义一个在本方法中都生效的局部变量 注意手动赋值null
        InputStream in = null;
        try {
//            InputStream in = new FileInputStream(new File("F:\\ready\\1.txt"))//创建file对象
            in = new FileInputStream("F:\\ready\\1.txt");

//            System.out.println(in.read());
            //2.开始读取
            /**read()每次调用都会读取一个字节，如果读到了数据的末尾，就会输出 -1 */
            int b;
            while((b=in.read()) != -1){
                System.out.println(b);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {//final修饰类不被继承，修饰常量不可修改，修饰方式不被重写
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
